<template>
    <el-dialog
        :before-close="resetForm"
        :close-on-click-modal="false"
        :visible.sync="visible"
        append-to-body
        title="编辑租户信息"
        width="1060px"
    >
        <div class="edit_page_div">
			<div>
				<el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="100px" style="float: left;">
					<el-form-item label="企业编号" prop="enterpriseCode">
						<el-input v-model="form.enterpriseCode" disabled placeholder="系统生成"
								  style="width: 300px;"></el-input>
					</el-form-item>
					<el-form-item label="企业名称" prop="enterpriseName">
						<el-input  style="width: 300px;" v-model="form.enterpriseName"></el-input>
					</el-form-item>
					<el-form-item label="管理账号" prop="accountNumber">
						<el-input  style="width: 300px;" v-model="form.accountNumber" :disabled="form.initializeFlag===1"></el-input>
					</el-form-item>
					<el-form-item label="密码" prop="password">
						<el-input style="width: 300px;" type="password" v-model="form.password" :disabled="form.initializeFlag===1"></el-input>
					</el-form-item>
				</el-form>
				<div class="page_tree" style="width: 600px; height: 500px; float: right; overflow: auto;">
					<el-tree
						ref="tree"
						v-loading="loading"
						:data="dataList"
						:expand-on-click-node="false"
						:props="defaultProps"
						default-expand-all
						node-key="id"
						show-checkbox
					>
						<div slot-scope="{ node, data }" class="tree_power">
							<div class="tree_power_name">{{ data.showName }}</div>
							<div class="tree_power_btn" style="margin-left: 20px;">
								<el-checkbox v-for="(item,index) in data.btnList" :key="item.id" v-model="item.checkFlag" :checked="item.checkFlag" :label="item.showName"></el-checkbox>
							</div>
						</div>
					</el-tree>
				</div>
			</div>
            <div class="edit_button" style="margin-top: 10px">
                <el-button v-loading.fullscreen.lock="loading" type="primary" @click="submit">保 存</el-button>
                <el-button type="primary" @click="resetForm">取 消</el-button>
            </div>
        </div>
    </el-dialog>
</template>

<script>
import RadiosOption from "@/components/radio/radiosOption.vue";

export default {
    name: "enterpriseEdit",
    components: {RadiosOption},
    props: {
        visible: Boolean,
        id: String,
		selectIdList: Array,
    },
    data() {
        return {
            loading: false,
            form: {
                enterpriseCode: '',
                enterpriseName: '',
                accountNumber: '',
				password: '',
            },
            rules: {
                enterpriseName: [{required: true, message: '', trigger: 'change'}],
                accountNumber: [{required: true, message: '', trigger: 'change'}],
				password: [{required: true, message: '', trigger: 'change'}],
            },
	
			dataList: [{
				childList: [],
				btnList: [],
			}],
			defaultProps: {
				children: 'childList',
				label: 'showName'
			},
        }
    },
    watch: {
        visible(newVal) {
            if (newVal) {
				this.getTreeList();
                if (this.id) {
                    this.getData();
                }
            }
        }
    },
    methods: {
		getTreeList() {
			this.loading = true;
			this.$axios.post("/CHILD_SYSTEM/systemPower/getEnterprisePowerList", {
                enterpriseId: this.id
            }).then(res => {
				this.dataList = res.data;
				this.$nextTick(() => {
					this.$refs.tree.setCheckedKeys(this.selectIdList);
				})
				this.loading = false;
			}).catch(() => {
				this.loading = false;
			})
		},
        getData() {
            this.loading = true;
            this.$axios.get("/CHILD_SYSTEM/systemEnterprise/getOneEntity/" + this.id).then(res => {
                this.form = res.data;
                this.loading = false;
            }).catch(() => {
                this.loading = false;
            })
        },
        submit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                	let res = this.$refs.tree.getCheckedKeys();
					this.getCheckedKeys(res, this.dataList);
					if (res.length === 0) {
						return this.$message.warning('至少选择一个菜单')
					}
					let idList = []
					res.forEach((i, n) => {
						if (!isNaN(i)) {
							idList.push(i);
						}
					})
					this.form.powerIdList = idList;
                    this.loading = true;
                    this.$axios.post("/CHILD_SYSTEM/systemEnterprise/" + (this.id ? "update" : "insert"), this.form).then(res => {
                        this.resetForm();
                        this.$message.success("保存功能！");
                        this.loading = false;
                    }).catch(() => {
                        this.loading = false;
                    })
                } else {
                    this.$message.error('表单填写有误');
                    return false;
                }
            });
        },
		getCheckedKeys(res, tree) {
			tree.forEach(node => {
				if (node.childList !== null && node.childList.length > 0) {
					this.getCheckedKeys(res, node.childList);
				}
				if (node.btnList !== null && node.btnList.length > 0) {
					node.btnList.forEach(temp => {
						if (temp.checkFlag) {
							res.push(temp.id)
						}
					})
				}
			})
		},
        resetForm() {
            this.$refs.form.resetFields();
            this.form = this.$options.data().form;
            this.$emit('update:visible', false);
            this.$emit('refreshList');
        }
    }
}
</script>

<style scoped>

</style>